Tasks = new Mongo.Collection("tasks");
console.log(5);

Tasks.allow({
  update: function(userId, task) { return ownsDocument(userId, task); },
  remove: function(userId, task) { return ownsDocument(userId, task); }
});

Tasks.deny({
  update: function(userId, task, fieldNames) {
    // may only edit the following two fields:
    return (_.without(fieldNames, 'text').length > 0);
  }
});

Tasks.deny({
  update: function(userId, task, fieldNames, modifier) {
    var errors = validateTask(modifier.$set);
    return errors.text;
  }
});

validateTask = function (task) {
  var errors = {};

  if (!task.text)
    errors.text = "Please fill in content";
  
  return errors;
}

Meteor.methods({
  addTask: function (text) {
    // Make sure the user is logged in before inserting a task
    if (! Meteor.userId()) {
      throw new Meteor.Error("not-authorized");
    }

console.log(6);
    Tasks.insert({
      text: text,
      createdAt: new Date(),
      owner: Meteor.userId(),
      username: Meteor.user().username
    });
console.log(6.1);
  },
  deleteTask: function (taskId) {
	  var task = Tasks.findOne(taskId);
      if (task.private && task.owner !== Meteor.userId()) {
        // If the task is private, make sure only the owner can delete it
        throw new Meteor.Error("not-authorized");
      }
    Tasks.remove(taskId);
  },
  setChecked: function (taskId, setChecked) {
	  var task = Tasks.findOne(taskId);
      if (task.private && task.owner !== Meteor.userId()) {
        // If the task is private, make sure only the owner can check it off
        throw new Meteor.Error("not-authorized");
      }
    Tasks.update(taskId, { $set: { checked: setChecked} });
  },
  setPrivate: function (taskId, setToPrivate) {
    var task = Tasks.findOne(taskId);
  
    // Make sure only the task owner can make a task private
    if (task.owner !== Meteor.userId()) {
      throw new Meteor.Error("not-authorized");
    }
  
    Tasks.update(taskId, { $set: { private: setToPrivate } });
  },
  updateTask: function (taskId, props) {
    var task = Tasks.findOne(taskId);
  
    // Make sure only the task owner can make a task private
    if (task.owner !== Meteor.userId()) {
      throw new Meteor.Error("not-authorized");
    }
  
    Tasks.update(taskId, { $set: props });
  }
});
console.log(5.1);
